package com.chaoxuan.uebt.activity;

import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.chaoxuan.uebt.R;
import com.chaoxuan.uebt.model.Order;
import com.chaoxuan.uebt.model.User;
import com.chaoxuan.uebt.model.Wallet;
import com.chaoxuan.uebt.utils.BaseSubscriber;
import com.chaoxuan.uebt.utils.Const;
import com.chaoxuan.uebt.utils.RetrofitUtils;
import com.chaoxuan.uebt.utils.CacheUtils;
import com.chaoxuan.uebt.utils.ToastUtils;

public class OrderDetailActivity extends BaseActivity implements View.OnClickListener{

    String id;
    TextView tvName,tvCount,tvMoney,tvStatus,tvSn,tvAddTime,tvEndTime,tvContactName,tvContactAddress,tvPost,tvPostNumber,tvKeyPassword,tvKeyNumber;
    ImageView img,imgOrderStatus;
    View view1,view2,view3,view4,view5;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_detail);
        initTitle();
        id=getIntent().getStringExtra("id");
        view1=findViewById(R.id.view1);
        view2=findViewById(R.id.view2);
        view3=findViewById(R.id.view3);
        view4=findViewById(R.id.view4);
        img=(ImageView) findViewById(R.id.img_order);
        imgOrderStatus=(ImageView)findViewById(R.id.img_order_status);
        tvName=(TextView)findViewById(R.id.tv_order_name);
        tvCount=(TextView)findViewById(R.id.tv_order_count);
        tvMoney=(TextView)findViewById(R.id.tv_order_money);
        tvStatus=(TextView)findViewById(R.id.tv_order_status);
        tvSn=(TextView)findViewById(R.id.tv_order_sn);
        tvAddTime=(TextView)findViewById(R.id.tv_order_add_time);
        tvEndTime=(TextView)findViewById(R.id.tv_order_end_time);
        tvContactName=(TextView)findViewById(R.id.tv_order_contact_name);
        tvContactAddress=(TextView)findViewById(R.id.tv_order_contact_address);
        tvPost=(TextView)findViewById(R.id.tv_order_post);
        tvPostNumber=(TextView)findViewById(R.id.tv_order_post_number);
        tvKeyNumber=(TextView)findViewById(R.id.tv_order_key_number);
        tvKeyPassword=(TextView)findViewById(R.id.tv_order_key_password);
        view5=findViewById(R.id.view5);
        view5.setOnClickListener(this);
        findViewById(R.id.btn_copy_id).setOnClickListener(this);
        findViewById(R.id.btn_copy).setOnClickListener(this);
        findViewById(R.id.btn_key_number).setOnClickListener(this);
        findViewById(R.id.btn_key_password).setOnClickListener(this);
        load();
    }

    @Override
    public void onClick(View v) {
        ClipboardManager cm = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
        switch (v.getId()){
            case R.id.view5:
                close();
                break;
            case R.id.btn_copy_id:
                cm.setText(tvSn.getText().toString());
                ToastUtils.showCustomToast("复制成功");
                break;
            case R.id.btn_copy:
                cm.setText(tvPostNumber.getText().toString());
                ToastUtils.showCustomToast("复制成功");
                break;
            case R.id.btn_key_number:
                cm.setText(tvKeyNumber.getText().toString());
                ToastUtils.showCustomToast("复制成功");
                break;
            case R.id.btn_key_password:
                cm.setText(tvKeyPassword.getText().toString());
                ToastUtils.showCustomToast("复制成功");
                break;
        }
    }

    private void load(){
        User user= CacheUtils.getInstance(this).getUser();
        subscribe(RetrofitUtils.getAPIService().orderDetail(user.getToken(), id, Const.DEVICE_TYPE), new BaseSubscriber<Order>() {
            @Override
            public void onSuccess(Order o) {
                Glide.with(getBaseContext()).load(o.getImg()).centerCrop().diskCacheStrategy(DiskCacheStrategy.ALL).placeholder(R.mipmap.default_info).error(R.mipmap.default_info).into(img);
                tvName.setText(o.getName());
                tvAddTime.setText(o.getAdd_time());
                tvEndTime.setText(o.getEnd_time());
                tvCount.setText(String.valueOf(o.getCount()));
                tvMoney.setText(o.getMoney());
                tvSn.setText(o.getOrder_sn());
                tvStatus.setText(o.getStatus());
                if(o.getStatus().equals("待发货")){
                    imgOrderStatus.setImageResource(R.mipmap.order9);
                }else if(o.getStatus().equals("待收货")){
                    imgOrderStatus.setImageResource(R.mipmap.order11);
                }else if(o.getStatus().equals("已完成")){
                    imgOrderStatus.setImageResource(R.mipmap.order12);
                }
                Log.d("---",TextUtils.isEmpty(o.getKey_number())+"");
                if(TextUtils.isEmpty(o.getKey_number())) {//实物订单
                    tvContactName.setText(o.getContact_name()+"    "+o.getContact_mobile());
                    tvContactAddress.setText(o.getContact_address());
                    tvPost.setText(o.getPost());
                    tvPostNumber.setText(o.getPost_number());
                    view3.setVisibility(View.VISIBLE);

                    view2.setVisibility(View.GONE);
                    view4.setVisibility(View.GONE);
                    if(o.getStatus().equals("待发货")){
                        view1.setVisibility(View.GONE);
                    }else{
                        view1.setVisibility(View.VISIBLE);
                    }
                    if(o.getStatus().equals("已完成")) {
                        view5.setVisibility(View.GONE);
                    }else{
                        view5.setVisibility(View.VISIBLE);
                    }
                }else{//虚拟订单
                    tvKeyNumber.setText(o.getKey_password());
                    tvKeyPassword.setText(o.getKey_number());
                    view1.setVisibility(View.GONE);
                    view2.setVisibility(View.VISIBLE);
                    view3.setVisibility(View.GONE);
                    view4.setVisibility(View.VISIBLE);
                    view5.setVisibility(View.GONE);
                }
            }
        });
    }

    private void close(){
        User user= CacheUtils.getInstance(this).getUser();
        if(user==null)return;
        subscribe(RetrofitUtils.getAPIService().endOrder(user.getToken(),id,Const.DEVICE_TYPE), new BaseSubscriber<Object>() {
            @Override
            public void onSuccess(Object data) {
                view5.setVisibility(View.GONE);
                ToastUtils.showCustomToast("已收货");
            }
        });
    }

}
